Examples of plotly

We are going to look at instacart dataset.

library(tidyverse)
library(p8105.datasets)
library(plotly)
data("instacart")

Plotly plots

boxplot

Among all department, when do customers usually place an order within a day?

y <- list(
  title = "Hour of a day"
)

instacart %>% 
  plot_ly(x = ~department, y = ~order_hour_of_day, type = "box") %>% 
  layout(yaxis = y)

bar graph

Among all departments, which department is the most popular for users?

y <- list(
  title = "number of order"
)

instacart %>% 
  count(department) %>% 
  mutate(
    department = fct_reorder(department,n)) %>% 
  plot_ly(
    x = ~department, y = ~n, color = ~department, 
    type = "bar", colors = "viridis") %>% 
  layout(yaxis = y)

violin plot

Is the size of the order related to days since prior order?

x <- list(
  title = "Days since prior order"
)

y <- list(
  title = "Items in an order"
)


instacart %>% 
  group_by(order_id) %>% 
  mutate(
    num_per_order = n(),
    text_label=str_c("Days:", days_since_prior_order, "\nItems in an order:", num_per_order)) %>% 
  distinct(order_id, num_per_order, days_since_prior_order, text_label) %>% 
  plot_ly(
    x = ~days_since_prior_order, y = ~num_per_order, text = ~text_label,
    type = "violin", colors = "viridis") %>% 
   layout(xaxis = x, yaxis = y)